<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input
          v-model="query.blurry"
          clearable
          placeholder="输入标题或者内容搜索"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-select
          v-model="query.verifyStatus"
          clearable
          size="small"
          placeholder="审核状态"
          class="filter-item"
          style="width: 115px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in dict.info_verify_status"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-select
          v-model="query.isEnable"
          clearable
          size="small"
          placeholder="状态"
          class="filter-item"
          style="width: 80px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in dict.data_status"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
    </div>
    <!--表单组件-->
    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible.sync="crud.status.cu > 0"
      :title="crud.status.title"
      width="850px"
    >
      <el-form ref="form" inline :model="form" :rules="rules" size="small" label-width="80px">
        <el-form-item label="标题" prop="name">
          <el-input v-model="form.name" style="width: 700px;" />
        </el-form-item>
        <el-form-item label="内容">
          <!-- 子组件将会在这里渲染出来  指定初始化的数据，以及监听数据更新的事件-->
          <SimpleEditor v-model="content" :is-clear="isClear" style="width: 700px;" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" style="width: 700px;" />
        </el-form-item>
        <el-form-item v-permission="permission.verify" label="审核状态">
          <el-select v-model="form.verifyStatus" filterable placeholder="请选择" style="width: 300px;">
            <el-option v-for="item in dict.info_verify_status" :key="item.id" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio v-for="item in dict.data_status" :key="item.id" v-model="form.isEnable" :label="item.value">{{ item.label }}</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="crud.cancelCU">取消</el-button>

        <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
      </div>
    </el-dialog>
    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      size="small"
      style="width: 100%;"
      @selection-change="crud.selectionChangeHandler"
    >
      <el-table-column type="selection" width="50" />
      <el-table-column prop="name" label="标题" width="190" />
      <el-table-column prop="content" label="内容" width="300">
        <template slot-scope="scope">
          <div><span v-html="scope.row.content.substring(0, scope.row.content.length > 40 ? 40 : scope.row.content.length)" /></div>
        </template>
      </el-table-column>
      <el-table-column prop="verifyStatus" label="审核状态">
        <template slot-scope="scope">
          <div v-if="scope.row.verifyStatus=='0'">待审核</div>
          <div v-if="scope.row.verifyStatus=='1'" style="color: limegreen">审核通过</div>
          <div v-if="scope.row.verifyStatus=='2'" style="color: #FF4949">审核未通过</div>
        </template>
      </el-table-column>
      <el-table-column prop="publisher" label="发布者">
        <template slot-scope="scope">
          <div>{{ scope.row.publisher.username }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="发布时间" width="135">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="reviewer" label="审核者">
        <template slot-scope="scope">
          <div>{{ scope.row.reviewer == null ? '' : scope.row.reviewer.username }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="reviewTime" label="审核时间" width="135">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reviewTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="isEnable" label="状态" fixed="right" width="50">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isEnable"
            active-color="#409EFF"
            inactive-color="#F56C6C"
            @change="changeEnabled(scope.row, scope.row.isEnable)"
          />
        </template>
      </el-table-column>
      <el-table-column v-permission="['admin','news:edit','news:del']" label="操作" width="120px" align="center" fixed="right">
        <template slot-scope="scope">
          <udOperation :data="scope.row" :permission="permission" />
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination />
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import crudNews from '@/api/anniversary/news'
import SimpleEditor from '@/components/SimpleEditor'
import CRUD, {
  presenter,
  header,
  form,
  crud
} from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = {
  id: null,
  name: null,
  userId: null,
  content: null,
  verifyStatus: '0',
  sort: 999,
  remark: null,
  isEnable: 'false'
}
export default {
  name: 'News',
  components: {
    pagination,
    crudOperation,
    rrOperation,
    udOperation,
    SimpleEditor
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['info_verify_status', 'data_status'],
  cruds() {
    return CRUD({
      title: '新闻资讯',
      url: 'api/news',
      sort: 'id,desc',
      crudMethod: { ...crudNews
      }
    })
  },
  data() {
    return {
      // 编辑器内容
      content: '',
      isClear: false, // 设置为true的时候，这个可以用this.wangEditorDetail=''来替代
      permission: {
        add: ['admin', 'news:add'],
        edit: ['admin', 'news:edit'],
        del: ['admin', 'news:del'],
        verify: ['admin', 'news:verify']
      },
      rules: {
        name: [{
          required: true,
          message: '标题不能为空',
          trigger: 'blur'
        }]
      },
      queryTypeOptions: [{
        key: 'name',
        display_name: '标题'
      },
      {
        key: 'content',
        display_name: '内容'
      }
      ]
    }
  },
  computed: {
    ...mapGetters([
      'user'
    ])
  },
  mounted() {
    this.content = '' // 设置富文本框默认显示内容
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      if (form.id == null) { form.userId = this.user.id }
      if (form.id != null && form.content != null) { this.content = form.content } else { this.content = '' }
      form.verifyStatus = `${form.verifyStatus}`
      form.isEnable = `${form.isEnable}`
    },
    // 提交前做的操作
    [CRUD.HOOK.afterValidateCU](crud) {
      crud.form.content = this.content
    },
    // 改变状态
    changeEnabled(data, val) {
      this.$confirm('此操作将 "' + this.dict.label.data_status[val] + '" ' + data.name + ', 是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        crudNews.edit(data).then(res => {
          this.crud.notify(this.dict.label.data_status[val] + '成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        }).catch(() => {
          data.isEnable = !data.isEnable
        })
      }).catch(() => {
        data.isEnable = !data.isEnable
      })
    }
  }
}
</script>

<style scoped>

</style>
